package zelix.hack.hacks;

import java.util.LinkedList;
import java.util.Queue;

import net.minecraft.entity.Entity;
import net.minecraft.network.play.client.CPacketUseEntity;
import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.utils.Entity301;
import zelix.utils.system.Connection.Side;
import zelix.utils.hooks.visual.ChatUtils;
import zelix.utils.Wrapper;

public class BlinkAttack extends Hack {

	public Entity301 entity301 = null;
	Queue<CPacketUseEntity> packets = new LinkedList<>();
	boolean send = false;

	public BlinkAttack() {
		super("BlinkAttack", HackCategory.COMBAT);
	}

	@Override
	public String getDescription() {
		return "Allows you to move without sending it to the server.";
	}

	@Override
	public boolean onPacket(Object packet, Side side) {
		if (side == Side.OUT && packet instanceof CPacketUseEntity) {

			ChatUtils.message("AttackPackets:" + String.valueOf(packets.size()));

			send = false;
			packets.add((CPacketUseEntity) packet);
			return send;
		} else {
			send = true;
		}
		return send;
	}

	@Override
	public void onEnable() {
		if (Wrapper.INSTANCE.player() != null && Wrapper.INSTANCE.world() != null) {
			this.entity301 = new Entity301(Wrapper.INSTANCE.world(), Wrapper.INSTANCE.player().getGameProfile());
			this.entity301.setPosition(Wrapper.INSTANCE.player().posX, Wrapper.INSTANCE.player().posY,
					Wrapper.INSTANCE.player().posZ);
			this.entity301.inventory = Wrapper.INSTANCE.inventory();
			this.entity301.rotationPitch = Wrapper.INSTANCE.player().rotationPitch;
			this.entity301.rotationYaw = Wrapper.INSTANCE.player().rotationYaw;
			this.entity301.rotationYawHead = Wrapper.INSTANCE.player().rotationYawHead;
			Wrapper.INSTANCE.world().spawnEntity(this.entity301);
		}
		super.onEnable();
	}

	@Override
	public void onDisable() {
		while (!packets.isEmpty())
			Wrapper.INSTANCE.player().connection.sendPacket(packets.poll());

		if (this.entity301 != null && Wrapper.INSTANCE.world() != null) {
			Wrapper.INSTANCE.world().removeEntity((Entity) this.entity301);
			this.entity301 = null;
		}
		super.onDisable();
	}
}
